/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.filter.spi;

import com.gotobject.filter.predicate.AbstractPredicate;
import com.gotobject.filter.predicate.Predicate;
import com.gotobject.filter.spi.Committed.Context;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class ExecutionContext {
	private ExecutionContext(){}

	public static <T> Predicate<T> any(Predicate<? super T> cond){
		@Committed(to = Context.ANY)
		class ErrantPredicate<T> extends AbstractPredicate<T> {
			private final Predicate<? super T> cond;
			ErrantPredicate(Predicate<? super T> cond){
				this.cond = cond;
			}


			public boolean apply(T thing) {
				return cond.apply(thing);
			}


			@Override
			public String toString() {
				return cond.toString();
			}
		}
		return new ErrantPredicate<T>(cond);
	}


	public static <T> Predicate<T> select(Predicate<? super T> cond){
		@Committed(to = Context.SELECT)
		class PostPredicate<T> extends AbstractPredicate<T> {
			private final Predicate<? super T> cond;
			PostPredicate(Predicate<? super T> cond){
				this.cond = cond;
			}


			public boolean apply(T thing) {
				return cond.apply(thing);
			}


			@Override
			public String toString() {
				return cond.toString();
			}
		}
		return new PostPredicate<T>(cond);
	}


	public static <T> Predicate<T> where(Predicate<T> cond){
		@Committed(to = Context.WHERE)
		class PrePredicate<T> extends AbstractPredicate<T> {
			private final Predicate<T> cond;
			PrePredicate(Predicate<T> cond){
				this.cond = cond;
			}


			public boolean apply(T thing) {
				return cond.apply(thing);
			}


			@Override
			public String toString() {
				return cond.toString();
			}
		}
		return new PrePredicate<T>(cond);
	}
}
